package com.xiaozhichao.databafflesys.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaozhichao.databafflesys.common.domain.ExceptionEnums;
import com.xiaozhichao.databafflesys.common.domain.PageHelper;
import com.xiaozhichao.databafflesys.common.domain.ResultVo;
import com.xiaozhichao.databafflesys.repository.model.BaffleApp;
import com.xiaozhichao.databafflesys.repository.model.BaffleInterface;
import com.xiaozhichao.databafflesys.repository.service.BaffleAppService;
import com.xiaozhichao.databafflesys.repository.service.BaffleInterfaceService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collection;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaozhichao
 * @since 2021-03-23
 */
@Controller
@RequestMapping("/baffleApp")
public class BaffleAppController {

    @Autowired
    BaffleAppService appService;
    @Autowired
    BaffleInterfaceService interfaceService;

    @RequestMapping("/selectPage")
    @ResponseBody
    public ResultVo selectPage(BaffleApp entry, PageHelper helper){
        IPage<BaffleApp> page = new Page<>(helper.getPage(),helper.getLimit());
        QueryWrapper<BaffleApp> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(!StringUtils.isEmpty(entry.getCode()),BaffleApp::getCode,entry.getCode())
                .like(!StringUtils.isEmpty(entry.getName()),BaffleApp::getName,entry.getName());
        IPage<BaffleApp> pageResult = appService.page(page, wrapper);
        return ResultVo.getResult(ExceptionEnums.SUCCESS,pageResult.getTotal(),pageResult.getRecords());
    }

    @RequestMapping("/edit")
    @ResponseBody
    public ResultVo edit(@RequestBody BaffleApp entry){
        BaffleApp old = appService.getById(entry.getId());
        if(Objects.isNull(old)){
            return ResultVo.getResult(ExceptionEnums.DO_FAIL);
        }

        // 验证是否已使用
        QueryWrapper<BaffleInterface> wrapperUse = new QueryWrapper<>();
        wrapperUse.lambda().eq(!StringUtils.isEmpty(old.getCode()),BaffleInterface::getAppCode,old.getCode());
        int countUse = interfaceService.count(wrapperUse);
        if(countUse > 0){
            return ResultVo.getResult(ExceptionEnums.DO_FAIL,"该code已有接口在使用，不能修改");
        }

        // 验证code唯一
        QueryWrapper<BaffleApp> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(!StringUtils.isEmpty(entry.getCode()),BaffleApp::getCode,entry.getCode())
                .ne(Objects.nonNull(entry.getId()),BaffleApp::getId,entry.getId());
        int countCode = appService.count(wrapper);
        if(countCode > 0){
            return ResultVo.getResult(ExceptionEnums.DO_FAIL,"该code码已存在");
        }

        // 验证name唯一
        wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(!StringUtils.isEmpty(entry.getName()),BaffleApp::getName,entry.getName())
                .ne(Objects.nonNull(entry.getId()),BaffleApp::getId,entry.getId());
        int countName = appService.count(wrapper);
        if(countName > 0){
            return ResultVo.getResult(ExceptionEnums.DO_FAIL,"该名称已存在");
        }

        // 更新
        UpdateWrapper<BaffleApp> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(BaffleApp::getId,entry.getId());
        appService.update(entry,updateWrapper);

        return ResultVo.getResult(ExceptionEnums.SUCCESS);
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResultVo add(@RequestBody BaffleApp entry){
        // 验证code唯一
        QueryWrapper<BaffleApp> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(!StringUtils.isEmpty(entry.getCode()),BaffleApp::getCode,entry.getCode())
                .ne(Objects.nonNull(entry.getId()),BaffleApp::getId,entry.getId());
        int countCode = appService.count(wrapper);
        if(countCode > 0){
            return ResultVo.getResult(ExceptionEnums.DO_FAIL,"该code码已存在");
        }

        // 验证name唯一
        wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(!StringUtils.isEmpty(entry.getName()),BaffleApp::getName,entry.getName())
                .ne(Objects.nonNull(entry.getId()),BaffleApp::getId,entry.getId());
        int countName = appService.count(wrapper);
        if(countName > 0){
            return ResultVo.getResult(ExceptionEnums.DO_FAIL,"该名称已存在");
        }

        // 保存
        appService.save(entry);

        return ResultVo.getResult(ExceptionEnums.SUCCESS);
    }

    @RequestMapping("/del")
    @ResponseBody
    public ResultVo del(@RequestBody Collection<String> codeList){
        // 验证是否已使用
        QueryWrapper<BaffleInterface> wrapperUse = new QueryWrapper<>();
        wrapperUse.lambda().in(!CollectionUtils.isEmpty(codeList),BaffleInterface::getAppCode,codeList);
        int countUse = interfaceService.count(wrapperUse);
        if(countUse > 0){
            return ResultVo.getResult(ExceptionEnums.DO_FAIL,"该code已有接口在使用，不能删除");
        }
        // 删除
        QueryWrapper<BaffleApp> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(!CollectionUtils.isEmpty(codeList),BaffleApp::getCode,codeList);
        appService.remove(wrapper);

        return ResultVo.getResult(ExceptionEnums.SUCCESS);
    }

    @RequestMapping("/list")
    @ResponseBody
    public ResultVo list(){
        return ResultVo.getResult(ExceptionEnums.SUCCESS,appService.list());
    }

}

